package com.winsdom.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.winsdom.domain.dto.FinishedThreeTableDTO;
import com.winsdom.domain.vo.*;
import com.winsdom.entity.Category;
import com.winsdom.result.Result;
import com.winsdom.service.ICategoryService;
import com.winsdom.service.IReadingRecordService;
import com.winsdom.service.IReadingTaskService;
import com.winsdom.service.IThreeTablesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@RestController
@RequestMapping("/meter/reading")
@Api(tags = "三表抄表管理")
@Slf4j
public class MeterReadingController {
    @Autowired
    private ICategoryService iCategoryService;
    @Autowired
    private IReadingTaskService iReadingTaskService;
    @Autowired
    private IThreeTablesService iThreeTablesService;
    @Autowired
    private IReadingRecordService iReadingRecordService;

    @GetMapping("/building")
    @ApiOperation(value = "获取登录账号下已授权且有任务的楼盘")
    public Result getBuilding() {
        List<Category> categories=iCategoryService.getBuildings();
        return Result.success(categories);
    }

    @GetMapping("/notreading")
    @ApiOperation(value = "获取未抄表任务")
    public Result getNotReading(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long pageNum,
            @Parameter(description = "页面大小") @RequestParam(defaultValue = "10") Long pageSize,
            @Parameter(description = "楼盘id") @RequestParam(required = false) Long buildingId) {
        log.info("pageNum:{}",pageNum);
        log.info("pageSize:{}",pageSize);
        log.info("楼盘id：{}",buildingId);
        Page<UnfinishedTaskVO> unfinishedTaskVO=iReadingTaskService.getNotReading(pageNum, pageSize,buildingId);
        log.info("获取未抄表任务:{}",unfinishedTaskVO);
        return Result.success(unfinishedTaskVO);
    }

    @GetMapping("/finished")
    @ApiOperation(value = "获取已抄表任务")
    public Result getFinished(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long pageNum,
            @Parameter(description = "页面大小") @RequestParam(defaultValue = "10") Long pageSize,
            @Parameter(description = "楼盘id") @RequestParam(required = false) Long buildingId) {
        log.info("pageNum:{}",pageNum);
        log.info("pageSize:{}",pageSize);
        log.info("楼盘id：{}",buildingId);
        Page<FinishedTaskVO> finishedTaskVO=iReadingTaskService.getFinished(pageNum, pageSize,buildingId);
        log.info("获取已抄表任务:{}",finishedTaskVO);
        return Result.success(finishedTaskVO);
    }

    @GetMapping("/building/export")
    @ApiOperation("导出楼盘下的已完成三表信息")
    public void exportThreeTableData(
            @Parameter(description = "楼盘id") @RequestParam Long buildingId,
            HttpServletResponse response){
        iThreeTablesService.getAllFinishedThreeTablesByBuilding(response, buildingId);
    }

    @GetMapping("/building/resource")
    @ApiOperation(value = "获取楼盘的资源")
    public Result getBuildingsAndResource(@Parameter(description = "楼盘id") @RequestParam Long buildingId) {
        List<BuildingResourceVO> buildingResourceVOS=iCategoryService.getBuildingResource(buildingId);
        return Result.success(buildingResourceVOS);
    }

    @GetMapping("/threetables/unfinished")
    @ApiOperation(value = "获取待抄表信息")
    public Result getUnfinishedThreeTables(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long pageNum,
            @Parameter(description = "页面大小") @RequestParam(defaultValue = "10") Long pageSize,
            @Parameter(description = "资源id") @RequestParam(required = false) Long resourceId,
            @Parameter(description = "楼盘id") @RequestParam(required = false) Long buildingId,
            @Parameter(description = "三表编号") @RequestParam(required = false) String number,
            @Parameter(description = "三表性质(0：水表 1；电表 2：气表)") @RequestParam(required = false) Integer type) {
        log.info("pageNum:{}",pageNum);
        log.info("pageSize:{}",pageSize);
        log.info("资源id:{}",resourceId);
        log.info("楼盘id:{}",buildingId);
        log.info("三表编号:{}",number);
        log.info("三表性质:{}",type);
        Page<UnfinishedThreeTableVO> unfinishedThreeTableVOPage=iThreeTablesService.getUnfinishedThreeTables(pageNum, pageSize,resourceId,buildingId,number,type);
        log.info("获取待抄表信息:{}",unfinishedThreeTableVOPage.getRecords());
        return Result.success(unfinishedThreeTableVOPage);
    }

    @GetMapping("/threetables/finished")
    @ApiOperation(value = "获取已抄表信息")
    public Result getFinishedThreeTables(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long pageNum,
            @Parameter(description = "页面大小") @RequestParam(defaultValue = "10") Long pageSize,
            @Parameter(description = "资源id") @RequestParam(required = false) Long resourceId,
            @Parameter(description = "楼盘id") @RequestParam(required = false) Long buildingId,
            @Parameter(description = "三表编号") @RequestParam(required = false) String number,
            @Parameter(description = "三表性质(0：水表 1；电表 2：气表)") @RequestParam(required = false) Integer type) {
        log.info("pageNum:{}",pageNum);
        log.info("pageSize:{}",pageSize);
        log.info("资源id:{}",resourceId);
        log.info("楼盘id:{}",buildingId);
        log.info("三表编号:{}",number);
        log.info("三表性质:{}",type);
        Page<FinishedThreeTableVO> finishedThreeTables=iThreeTablesService.getFinishedThreeTables(pageNum, pageSize,resourceId,buildingId,number,type);
        log.info("获取已抄表信息:{}",finishedThreeTables.getRecords());
        return Result.success(finishedThreeTables);
    }

    @PostMapping("/finished")
    @Operation(summary = "修改已抄表信息", description = "修改已抄表信息")
    public Result updateOrg(@RequestBody FinishedThreeTableDTO finishedThreeTableDTO) {
        log.info("接收新修改的抄表信息：{}",finishedThreeTableDTO);
        iReadingRecordService.updateFinished(finishedThreeTableDTO);
        return Result.success("已抄表信息修改成功");
    }

    @PostMapping("/unfinished")
    @Operation(summary = "批量抄表", description = "批量抄表")
    public Result updateOrg(@RequestBody List<FinishedThreeTableDTO> finishedThreeTableDTOs) {
        log.info("接收批量抄表信息：{}", finishedThreeTableDTOs);
        iReadingRecordService.updateUnFinishedBatch(finishedThreeTableDTOs);
        return Result.success("批量抄表成功");
    }

}
